import { asyncRouterMap, constantRouterMap } from '@/router'
/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.indexOf(role) >= 0)
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
// function filterAsyncRouter(asyncRouterMap, roles) {
//   const accessedRouters = asyncRouterMap.filter(route => {
//     if (hasPermission(roles, route)) {
//       if (route.children && route.children.length) {
//         route.children = filterAsyncRouter(route.children, roles)
//       }
//       return true
//     }
//     return false
//   })
//   return accessedRouters
// }
/**
 * 递归过滤异步路由表，返回对应的路由组件
 * @param list
 * @param name
 */
function getComponent(list, name) {
  for (var i in list) {
    var tempComponnet = null
    if (name === list[i].path) {
      return list[i].component
    } else {
      if (list[i].children && list[i].children.length > 0) {
        tempComponnet = getComponent(list[i].children, name)
        if (tempComponnet != null) {
          return tempComponnet
        }
      }
    }
  }
  return tempComponnet
}

/**
 * 递归过后台权限路由，通过URL查找对应的组件
 * @param RouteList
 */
function getRouteNode(RouteList) {
  var temp_route = []
  for (var i in RouteList) {
    if (RouteList[i].component === null || RouteList[i].component === '') {
      return false
    }
    console.log(RouteList[i].component)
    var temp_routeNode = {
      path: RouteList[i].component,
      component: getComponent(asyncRouterMap, RouteList[i].component)
      // redirect: RouteList[i].RedirectURL,
      // hidden: !RouteList[i].IsShow,
      // name: 'menu' + RouteList[i].id,
      // meta: {
      //  title: RouteList[i].name,
      //  icon: RouteList[i].icon
      // }
    }
    if (RouteList[i].type === 2 || RouteList[i].type === 1) {
      temp_routeNode.name = RouteList[i].id
      temp_routeNode.meta = {
        title: RouteList[i].name,
        icon: RouteList[i].icon
      }
    }
    if (RouteList[i].type === 0) {
      temp_routeNode.meta = {
        title: RouteList[i].name,
        icon: RouteList[i].icon
      }
    }
    if (RouteList[i].type === 2) {
      // console.log(RouteList[i].URL, '====================', RouteList[i].RedirectURL)
      temp_routeNode.hidden = true
    }
    // console.log('1234567890' + RouteList[i])
    if (RouteList[i].children !== null) {
      temp_routeNode.children = getRouteNode(RouteList[i].children)
    }
    if (RouteList[i].children.length === 0) {
      delete temp_routeNode.children
    }
    temp_route.push(temp_routeNode)
  }
  return temp_route
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        // const { roles } = data
        const accessedRouters = getRouteNode(data.data.data)
        console.log('========accessedRouters===========', accessedRouters)
        // let accessedRouters
        // if (roles.indexOf('admin') >= 0) {
        //   accessedRouters = asyncRouterMap
        // } else {
        //   accessedRouters = asyncRouterMap
        //   // accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
